<?
class Tbl_agenda extends CI_Model {
    var $tabel;

	//===============ADMIN================//
     function __construct(){
         // Call the Model constructor
         parent::__construct();
        $this->tabel = 'tbl_agenda';
	}
	  
	function load_admin($data,$posisi){
	$conditionQuery = "";
	if($data['deskripsi']!="null" || $data['deskripsi']!=null || $data['deskripsi']!=""){
		if($data['kategori']=="date"){
			$conditionQuery = $conditionQuery."WHERE date LIKE '";
			if($data['tahun']!="0")
				$conditionQuery = $conditionQuery.$data['tahun'];
			else
				$conditionQuery = $conditionQuery."____";
			$conditionQuery = $conditionQuery."-";
			if($data['bulan']!="0")
				$conditionQuery = $conditionQuery.$data['bulan'];
			else
				$conditionQuery = $conditionQuery."__";
			$conditionQuery = $conditionQuery."-";
			if($data['hari']!="0")
				$conditionQuery = $conditionQuery.$data['hari'];
			else
				$conditionQuery = $conditionQuery."__";
			$conditionQuery = $conditionQuery."%'";
		}
		elseif($data['deskripsi']!="null"){
			$conditionQuery = $conditionQuery."WHERE ".$data['kategori']." LIKE '%".$data['deskripsi']."%'";
		}
	}

	if(!is_numeric($posisi))
    {
      $posisi='0';
      $query=$this->db->query("SELECT *,DATE_FORMAT(date, '%e %M %Y') as newdate FROM ".$this->tabel." $conditionQuery LIMIT $posisi,10");
      return $query->result();
    }
	if(empty($posisi)) {$posisi = '0';}
  
		$query=$this->db->query("SELECT *,DATE_FORMAT(date, '%e %M %Y') as newdate FROM ".$this->tabel." $conditionQuery LIMIT $posisi,10");
		return $query->result();
	}
	
	function edit($id){
      $query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE id='$id'");
      return $query->result();
	 }
	
	function hitung_all($data){ 
		$conditionQuery = "";
		if($data['deskripsi']!="null" || $data['deskripsi']!=null || $data['deskripsi']!=""){
			if($data['kategori']=="date"){
				$conditionQuery = $conditionQuery."WHERE date LIKE '";
				if($data['tahun']!="0")
					$conditionQuery = $conditionQuery.$data['tahun'];
				else
					$conditionQuery = $conditionQuery."____";
				$conditionQuery = $conditionQuery."-";
				if($data['bulan']!="0")
					$conditionQuery = $conditionQuery.$data['bulan'];
				else
					$conditionQuery = $conditionQuery."__";
				$conditionQuery = $conditionQuery."-";
				if($data['hari']!="0")
					$conditionQuery = $conditionQuery.$data['hari'];
				else
					$conditionQuery = $conditionQuery."__";
				$conditionQuery = $conditionQuery."%'";
			}
			elseif($data['deskripsi']!="null"){
				$conditionQuery = $conditionQuery."WHERE ".$data['kategori']." LIKE '".$data['deskripsi']."'";
			}
		}
	
		$dataModel = $this->db->query("SELECT * FROM ".$this->tabel." ".$conditionQuery."");
		$banyak= $this->db->affected_rows($dataModel);
		return $banyak;
	}
	
	function add($judul,$isi,$date,$tipe){
		$array = array('date' => $date,'judul' => $judul,'isi' => $isi, 'tipe_agenda' => $tipe);
        $this->db->insert($this->tabel, $array);
    }
	 
	function update($id,$judul,$isi,$date ,$tipe) {
		$this->db->query("UPDATE ".$this->tabel." SET judul='$judul', isi='$isi', date='$date', tipe_agenda='$tipe' WHERE id=$id");
    }
	  
	 function delete($id) {
	 $this->db->query("DELETE FROM ".$this->tabel." WHERE id=$id");
    }
	
	function load_pengumuman_search($pengumuman){
	$posisi = $this->uri->segment(3);
    if(!is_numeric($posisi))
    {
      $posisi='0';
      $query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE judul LIKE '%$pengumuman%' LIMIT $posisi,10");
      return $query->result();
    }
	if(empty($posisi)) {$posisi = '0';}
  
		$query=$this->db->query("SELECT * FROM ".$this->tabel." WHERE judul LIKE '%$pengumuman%'  LIMIT $posisi,10");
		return $query->result();
	}
	
	function hitung_where($pengumuman){ 
		$data = $this->db->query("SELECT * FROM ".$this->tabel." WHERE judul LIKE '%$pengumuman%'");
		$banyak= $this->db->affected_rows($data);
		return $banyak;
	}
	
	//===============GUEST================//
	function guest_agenda($posisi){
    if(!is_numeric($posisi))
    {
      $posisi='0';
      $query=$this->db->query("SELECT *,DATE_FORMAT(date, '%e %M %Y) AS newDate FROM ".$this->tabel." WHERE date > now() ORDER BY date DESC LIMIT $posisi,15");
      return $query->result();
    }
	if(empty($posisi)) {$posisi = '0';}
		$query=$this->db->query("SELECT *,DATE_FORMAT(date, '%e %M %Y') AS newDate FROM ".$this->tabel." WHERE date > now() ORDER BY date DESC LIMIT $posisi,15");
		return $query->result();
	}
	
	function searchAgendaGuest($key){
	   $query = $this->db->query("SELECT *,DATE_FORMAT(date, '%e %M %Y') AS newdate FROM ".$this->tabel." WHERE judul RLIKE '$key' OR isi RLIKE '$key'");
	   return $query->result();
    }
	
	function getLatestAgenda(){
		$query=$this->db->query("SELECT *, DATE_FORMAT(date, '%e %M %Y') as newdate FROM ".$this->tabel." WHERE date > now() ORDER By date DESC LIMIT 2");
		return $query->result();
	}
}
?>